//
//  ZBNavigationItem.m
//  YouHui
//
//  Created by  on 11-11-24.
//  Copyright (c) 2011年 netease. All rights reserved.
//

#import "ZBNavigationItem.h"
#import "CommonColor.h"

@implementation ZBNavigationItem 

@synthesize titleView=_titleView;
@synthesize rightView=_rightView;
@synthesize leftView=_leftView;
@synthesize delegate=_delegate;
@synthesize titleLabel=_titleLabel;
@synthesize isLoading=_isLoading;
@synthesize hideTitle=_hideTitle;
@dynamic title;

-(void)dealloc {
    [_titleView release];
    [_rightView release];
    [_titleLabel release];
    [super dealloc];
}

-(void)setTitleView:(UIView *)titleView
{
    [titleView retain];
    [_titleView release];
    _titleView = titleView;
    if (_delegate) {
        [_delegate navigationItemUpdated:self]; 
    }
}

-(void)setRightView:(UIView *)rightView
{
    [rightView retain];
    [_rightView release];
    _rightView = rightView;
    if (_delegate) {
        [_delegate navigationItemUpdated:self]; 
    }
}

-(void)setLeftView:(UIView *)leftView
{
    [leftView retain];
    [_leftView release];
    _leftView = leftView;
    if (_delegate) {
        [_delegate navigationItemUpdated:self]; 
    }
}

-(void)setTitle:(NSString *)title
{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.font = [UIFont boldSystemFontOfSize:20.0];
        _titleLabel.textColor =[CommonColor darkBrown];
        _titleLabel.adjustsFontSizeToFitWidth = YES;
    }
    CGSize textSize = [title sizeWithFont:_titleLabel.font forWidth:140.0f lineBreakMode:UILineBreakModeWordWrap];
    CGRect rect = _titleLabel.frame;
    rect.size = textSize;
    _titleLabel.frame = rect;
    _titleLabel.text = title;
    
    if (_delegate) {
        [_delegate navigationItemUpdated:self]; 
    }
}

-(void)setIsLoading:(BOOL)value {
	if (_isLoading!=value) {
		_isLoading=value;
		if (_delegate&&_isLoading) {
			[_delegate loadingStart:self]; 
		}
		if (_delegate&&!_isLoading) {
			[_delegate loadingComplete:self]; 
		}
	}
}

-(void)setHideTitle:(BOOL)hideTitle {
	if (_hideTitle!=hideTitle) {
		_hideTitle=hideTitle;
        if (_delegate) {
            [_delegate navigationItemUpdated:self]; 
        }
	}    
}

-(NSString*)title {
    if (_titleLabel) {
        return _titleLabel.text;
    }
    return nil;
}

@end
